<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!--返回错误信息-->
<?php echo validation_errors(); ?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php
    $title = [
        'title' => '商品管理',
    ];
    $this->load->view('/admin/common/header', $title);
    ?>
</head>

<body data-type="widgets">
<script src="/assets/js/theme.js"></script>
<div class="am-g tpl-g">

<!--    头部   侧边菜单-->
    <?php $this->load->view('/admin/common/bodyLeft'); ?>
    <script type="text/javascript">
    	$(".js-sidebar-nav>li:nth-child(8)>ul").show(80);
    	$(".js-sidebar-nav>li:nth-child(8)>a>span").addClass("sidebar-nav-sub-ico-rotate");
    	$(".js-sidebar-nav>li:nth-child(8)>ul>li:nth-child(1)>a").addClass("active");
    </script>
    <!-- 内容区域 -->
    <div class="tpl-content-wrapper">
        <div class="row-content am-cf">
            <div class="row">
                <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
                    <div class="widget am-cf">
                        <div class="widget-head am-cf">
                            <div class="widget-title  am-cf">商品管理</div>
                        </div>
                        <form action="/admin/goods/remove" id="form_admin" method="post">

                        <div class="widget-body  am-fr">
                            <div class="am-u-md-12 ">
                                <div class="am-u-md-1 am-u-lg-1">
                                    <label class=" col-sm-centered am-md-text-right am-padding-left-0">商品分类</label>
                                </div>
                                <div class="am-u-md-2 am-u-lg-2">
                                    <div class="am-form-group tpl-table-list-select">
                                        <select data-am-selected="{btnSize: 'sm',btnWidth: '100%'}" id="firstClassify" class="unlock">
                                            <option value=""></option>
                                            <option value="2">物料包</option>
                                            <option value="3">书籍</option>
                                            <option value="4">机器耗材</option>
                                            <option value="8">供应商商品</option>
                                            <option value="9">打印机租赁</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="am-u-md-2 am-u-lg-2">
                                    <div class="am-form-group tpl-table-list-select">
                                        <select data-am-selected="{btnSize: 'sm',btnWidth: '100%'}" id="secondClassify">
                                            <option value=""></option>
                                        </select>
                                    </div>
                                </div>
                                <div class="am-u-md-2 am-u-lg-2">
                                    <div class="am-form-group tpl-table-list-select">
                                        <select data-am-selected="{btnSize: 'sm',btnWidth: '100%'}" id="thirdClassify">
                                            <option value=""></option>
                                        </select>
                                    </div>
                                </div>
                                <div class="am-u-md-4 am-u-lg-3">
                                    <div class="am-form-group am-input-group am-input-group-sm tpl-form-border-form cl-p">
    <!--                                    <form action="/admin/user/search" method="get">-->
                                        <input type="text" class="am-form-field" placeholder="请输入商品名称" name="search" id="searchValue">
                                        <span class="am-input-group-btn">
                                            <a href="javascript:;" id="search" class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search">搜索</a>
                                        </span>
    <!--                                    </form>-->
                                    </div>
                                </div>
                                
                            </div>
                            <div class="am-u-sm-12">
                                    <div class="am-btn-toolbar am-u-sm-12">
                                        <div class=" am-btn-group-xs  am-fl">
                                            <a href="javascript:;" onclick="yi()" class="am-btn am-btn-primary"><span class="am-icon-download"></span> 导出</a>
                                            <a href="/admin/goods/addView" class="am-btn am-btn-primary"><span class="am-icon-plus"></span> 新增商品</a>
                                        </div>
                                        <div class="am-btn-group-xs am-fr">
                                            <a href="javascript:;"  class="am-btn  am-btn-danger all-delete"><span class="am-icon-trash"></span> 批量删除</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="am-u-sm-12">
                                <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black"
                                       id="example-r">
                                    <thead>
                                    <tr>
                                        <th><input type="checkbox" class="checkall" id="checkallBottom">全选</th>
                                        <th>序号</th>
                                        <th>商品编码</th>
                                        <th>商品名称</th>
                                        <th>商品图片</th>
                                        <th>商品规格</th>
                                        <th>库存</th>
                                        <th>商品分类</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody class="choose-box">
                                    <?php
//                                    print_r($list);
//                                    die();
                                    if($list){
                                        foreach ($list as $v) {
//                                        2物料包、3书籍、4机器耗材、9打印机租赁、8供应商商品
                                            if(!isset($v['file_path'])){
                                                $v['file_path'] = '默认图片';
                                            }
                                            echo '<tr class="gradeX">
                                            <td>
                                                <label class="am-checkbox-inline">
                                                  <input type="checkbox" value='.$v['id'].' name="del_id[]" class="checkitem">
                                                </label>
                                             </td>
                                             <td>'.$v['id'].'</td>
                                             <td>'.$v['product_no'].'</td>
                                             <td>'.$v['name'].'</td>
                      
                                           <td><img src="'.$v['file_path'].'" class="tpl-table-line-img" onerror="this.onerror=null;this.src=\'/assets/img/default.jpg\'" alt=""></td>';

                                            if(isset($v['isSpecification']) && $v['isSpecification'] == 1){
                                                echo '<td><button type="button" class="am-btn am-btn-default am-radius specification-btn" data-am-modal="{target: \'#addSpecification\', closeViaDimmer: 0, width: 500, height: 400}">商品规格+</button></td>';
                                            }else{
                                                echo '<td></td>';
                                            }

                                             echo '<td>'.$v['stock'].'</td>';
                                            switch ($v['one_category']){
                                                case 2:
                                                    echo '<td>物料包</td>';
                                                    break;
                                                case 3:
                                                    echo '<td>书籍</td>';
                                                    break;
                                                case 4:
                                                    echo '<td>机器耗材</td>';
                                                    break;
                                                case 8:
                                                    echo '<td>供应商商品</td>';
                                                    break;
                                                case 9:
                                                    echo '<td>打印机租赁</td>';
                                                    break;

                                            }

                                            switch ($v['source']){
                                                case 'ist':
                                                    echo '<td data-id="'.$v['id'].'">
                                                <div class="tpl-table-black-operation">
                                                 <a href="/admin/goods/editView?id='.$v['id'].'">
                                                        <i class="am-icon-edit"></i> 编辑
                                                    </a>
                                                        <a href="#" class="tpl-table-black-operation-del" data-id="'.$v['id'].'">
                                                    <i class="am-icon-trash"></i> 删除
                                                        </a>
                                                    </div>
                                                    </td>
                                              </tr>';
                                                    break;
                                                case '51ck':
                                                    echo '<td data-id="'.$v['ckId'].'">
                                                <div class="tpl-table-black-operation">
                                               
                                                    </div>
                                                    </td>
                                              </tr>';

                                            }
                                        }
                                    }else{
                                        echo '';
                                    }


                                    ?>
                                    </tbody>
                                </table>
                            </div>
                            <div class="am-u-lg-12">
                                <div class="am-u-sm-centered">
                                    <ul class="am-pagination am-pagination-centered" id="paging">
                                    </ul>
                                </div>
                            </div>
                        </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
<!-- 模态框 -->
<div class="am-modal am-modal-no-btn" tabindex="-1" id="addSpecification">
    <div class="am-modal-dialog">
        <div class="am-modal-hd">规格值详情
            <a href="javascript: void(0)" class="am-close am-close-spin" data-am-modal-close>&times;</a>
        </div>
        <div class="am-modal-bd">
            <table class="am-table am-table-bordered am-table-radius am-table-striped am-table-hover">
                <thead>
                    <tr>
                        <th style="width: 50%">规格值</th>
                        <th style="width: 50%">单品编码</th>
                    </tr>
                </thead>
                <tbody class="specification-box">
                    
                </tbody>
            </table>
        </div>
        <div class="btn-box">
            <button type="button" class="am-btn am-btn-primary" id="confirmSpecification" data-am-modal-close>确认</button>
        </div>

    </div>
</div>

<!-- 模拟confirm -->
<div class="am-modal am-modal-confirm" tabindex="-1" id="my-confirm">
  <div class="am-modal-dialog">
    <div class="am-modal-bd">
      确定要删除记录吗？
    </div>
    <div class="am-modal-footer">
      <span class="am-modal-btn" data-am-modal-cancel>取消</span>
      <span class="am-modal-btn" data-am-modal-confirm>确定</span>
    </div>
  </div>
</div>

<!-- alert -->
<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert">
  <div class="am-modal-dialog">
    <div class="am-modal-hd">提示</div>
    <div class="am-modal-bd">
      请选择删除项！
    </div>
    <div class="am-modal-footer">
      <span class="am-modal-btn">确定</span>
    </div>
  </div>
</div>

<script>
  //   function confirm_delete() {
		// if(confirm('您确定要删除吗?')){$('#form_admin').submit();}
  //   }
    
    function search123() {
        var v = document.getElementById('search').value;
        console.log(v);

//        console.log(document.getElementById('searchData').value);
        window.location.href='?page=1'+'&search='+v

    }
</script>
<script>
	
    /* 全选 */
    $('.checkall').click(function(){
        $('.checkitem').prop('checked', this.checked);
    });

        //批量删除
    $(".all-delete").on("click",function(){
        if(!$(".checkitem:checked").length){
            $("#my-alert").modal();
            return;
        }
        $('#my-confirm').modal({
            onConfirm: function(options) {
                $('#form_admin').submit();
            },
            onCancel: function() {
            }
        });
    });
    //单个删除
    $(".tpl-table-black-operation-del").on("click",function(){
        var thisId = $(this).data("id");
        $('#my-confirm').modal({
            onConfirm: function(options) {
                window.location.href = "/admin/goods/remove?id=" + thisId;
            },
            onCancel: function() {
            }
        });
    });
  



    $(document).on("click",".specification-btn",function(){
        var thisTr = $(this).parents(".gradeX");                        //点击的本行tr
        var productId = thisTr.children("td:last-child").data("id");   //传给后台的商品ID
        console.log(productId)

        var specificationBox = $(".specification-box");

        specificationBox.empty();
        $.ajax({
            url:"/api/goods/getGoodsIdByData",
            data:{
                productId:productId
            },
            dataType:"json",
            type:"post",
            success:function(data){
//                console.log(data);
                var tr = "";
                for(var i = 0;i < data.length; i++) {
                    tr += '<tr>'
                            +'<td>'+ data[i].specification_group_names + '</td>'
                            +'<td>'+ data[i].group_no + '</td>'
                        + '</tr>';
                }

                specificationBox.html(tr);
            },
            error:function(data){
                console.log(data);
            }
        });
    });

    var oneCategory,
        twoCategory,
        threeCategory,
        search,
        page;

    $("#search").on("click",function(){

        oneCategory = $("#firstClassify").val();
        twoCategory = $("#secondClassify").val();
        threeCategory = $("#thirdClassify").val();
        search = $("#searchValue").val();
        page = 1;

        chooseGoods(oneCategory,twoCategory,threeCategory,search,page);

    });

    //商品分类,级联菜单  及  筛选商品 
    selectChange("#firstClassify","#secondClassify","/api/goods/twoCategory","one");
    selectChange("#secondClassify","#thirdClassify","/api/goods/threeCategory","two");
    selectChange("#thirdClassify","","","");

    //商品分类,级联菜单  及  筛选商品 
    function selectChange(changeId,nextSelect,url,changeNum){
        $(document).on("change" , changeId + ".unlock" , function() {

            //有这个class时，移除，不走下面代码
            if($(changeId).hasClass("noAjax")){
                $(changeId).removeClass("noAjax");
                return false;
            }

            if(changeId == "#thirdClassify"){
                //筛选商品
                oneCategory = $("#firstClassify").val();
                twoCategory = $("#secondClassify").val();
                threeCategory = $("#thirdClassify").val();
                search = $("#searchValue").val();
                page = 1;

                chooseGoods(oneCategory,twoCategory,threeCategory,search,page);
                return false;
            }

            //级联菜单
            var categoryValue = $(changeId).val();
            var data = changeNum + "Category=" + categoryValue;
            var option = "<option value=''><optian>";

            $.ajax({
                type: "POST",
                url : url,
                data: data,
                dataType: "json",
                success: function(data){
                    console.log(data)
                    for(var i = 0; i<data.length; i++){
                        if(data[i].product_categoryId){
                            option += '<option value="'+ data[i].product_categoryId +'">'+ data[i].name +'</option>';

                        }else{
                            option += '<option value="'+ data[i].id +'">'+ data[i].name +'</option>';
                        }
                    }
                    $(nextSelect).removeClass("unlock");
                    $(nextSelect).html(option);

                    setTimeout(function() {
                        $(nextSelect).addClass("unlock");
                    }, 0);

                    // 在第三个分类有option前提下，换第一个分类，给第三个分类清空，会触发change事件，加class，让第三个分类有class时候不请求ajax
                    if(changeId == "#firstClassify" && $("#thirdClassify option").length != 1){
                        $("#thirdClassify").empty().addClass("noAjax");
                    }
                },
                complete:function() {
                    //筛选商品
                    oneCategory = $("#firstClassify").val();
                    twoCategory = $("#secondClassify").val();
                    threeCategory = $("#thirdClassify").val();
                    search = $("#searchValue").val();
                    page = 1;

                    chooseGoods(oneCategory,twoCategory,threeCategory,search,page);

                },
                error: function (data) {
                    // alert(XMLHttpRequest.status);
                    // alert(XMLHttpRequest.readyState);
                    // alert(textStatus);
                }
            });


        });
    }



    function chooseGoods(oneCategory,twoCategory,threeCategory,search,page){
        $.ajax({
            type: "GET",
            url : "/api/goods/search",
            data: {
                oneCategory: oneCategory,
                twoCategory: twoCategory,
                threeCategory: threeCategory,
                search: search,
                page: page
            },
            dataType: "json",
            beforeSend:function(){
                $(".my-dark").css("display","block");
            },
            success: function(msg){
                var data = msg['0'];
                var list,
                    specificationBtn,
                    category;
                console.log(data);
                if(!data[0]){
                    list += '<tr class="gradeX"><td colspan="9">暂无数据</td></tr>'
                }else{
                    for(var i = 0 ; i < data.length;i++ ){
                        specificationBtn = data[i].isSpecification ? '<button type="button" class="am-btn am-btn-default am-radius specification-btn" data-am-modal="{target: \'#addSpecification\', closeViaDimmer: 0, width: 500, height: 400}">商品规格+</button>' : '';
                        category = data[i].one_category == 2 ? "物料包" : 
                                        data[i].one_category == 3 ? "书籍" :
                                            data[i].one_category == 4 ? "机器耗材" :
                                                data[i].one_category == 8 ? "供应商商品" : "打印机租赁";
                        list += '<tr class="gradeX">'
                                +'<td>'
                                    + '<label class="am-checkbox-inline">'
                                        + '<input type="checkbox" value="'+ data[i].id +'" name="del_id[]" class="checkitem">'
                                    + '</label>'
                                + '</td>'
                                + '<td>'+ data[i].id +'</td>'
                                + '<td>'+ data[i].product_no +'</td>'
                                + '<td>'+ data[i].name +'</td>'
                                + '<td>'
                                    + '<img src="'+ data[i].file_path +'" class="tpl-table-line-img" onerror="this.onerror=null;this.src=\'/assets/img/default.jpg\'" alt="">'
                                + '</td>'
                                + '<td>'
                                    + specificationBtn
                                + '</td>'
                                + '<td>'+ data[i].stock +'</td>'
                                + '<td>'+ category +'</td>';
                        if(data[i].source == "51ck"){
                            list += '<td data-id="'+ data[i].ckId +'">'
                                    + '<div class="tpl-table-black-operation">'
                                    + '</div>'
                                  + '</td>';
                        }else{
                            list += '<td data-id="'+ data[i].id +'">'
                                    + '<div class="tpl-table-black-operation">'
                                        + '<a href="/admin/goods/editView?id='+ data[i].id +'">'
                                            + '<i class="am-icon-edit"></i> 编辑'
                                        + '</a>'
                                        + '<a href="#" class="tpl-table-black-operation-del" data-id="'+ data[i].id +'">'
                                            + '<i class="am-icon-trash"></i> 删除'
                                        + '</a>'
                                    + '</div>'
                                  + '</td>';
                        }
                        list += '</tr>';
                    }
                }
                $(".choose-box").html(list);

                //分页
                //设置分页信息  
                var pageOptions = {  
                    AllowPaging: true,  
                    PageIndex: msg.this_page,      //设置当前页码  
                    PageSize: msg.pageNum,       //设置分页大小  
                    RecordCount: msg.total_num,  //设置数据总数  
                    // TotalPage: ,      //设置总页数
                    showPageCount: 4,  
                    onPageClick: function(pageIndex) {  
                        chooseGoods(oneCategory,twoCategory,threeCategory,search,pageIndex);
                    }  
                }  
                //初始化分页栏  
                $('#paging').pageBar(pageOptions); 

            },
            complete:function(){
                $(".my-dark").css("display","none");
            },
            error: function (data) {  
               //  alert(XMLHttpRequest.status);
               //  alert(XMLHttpRequest.readyState);
               //  alert(textStatus);
            }
        });
    }


</script>

<script src="/assets/js/amazeui.min.js"></script>
<script src="/assets/js/amazeui.datatables.min.js"></script>
<script src="/assets/js/dataTables.responsive.min.js"></script>
<script src="/assets/js/app.js"></script>
<script src="/assets/js/page.js"></script>
<script>
    $(document).ready(function() {  
        //设置分页信息  
        var pageOptions = {  
            AllowPaging: true,  
            PageIndex: <?php echo $_GET['page'] ?>,     //设置当前页码  
            PageSize: <?php echo $pageNum ?>,           //设置分页大小  
            RecordCount: <?php echo $totalNum ?>,       //设置数据总数  
            // TotalPage: ,      //设置总页数
            showPageCount: 4,  
            onPageClick: function(pageIndex) {  
                
                var page = parseInt(pageIndex);

                window.location.href = "?page="+ page; 
                
                
            }  
        }  
        //初始化分页栏
        $('#paging').pageBar(pageOptions);      
    })  
</script>
</body>

</html>